var xmlhttp;
var xml;

function getSelectedIndex(dropDownList, value)
{
    // Loop through all the items
    for (var i = 0; i < dropDownList.options.length; i++)
    {
        if (dropDownList.options[i].value == value)
        {
            // item is found, return the index number
            return i;
        }
    }

    // if this point was reached then item was not found
    return -1;
}

function getXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }

    return null;
}

function getComponents(form)
{
    var catId = form.category.options[form.category.options.selectedIndex].value;
    var url = "ajax/AjaxComponents.php?action=getCompList&cat=" + catId;

    clearFormData(form);

    xmlhttp = getXmlHttpObject();
    xmlhttp.onreadystatechange = categoryStateChanged;
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
}

function getComponent(form)
{
    var compId = form.component.options[form.component.options.selectedIndex].value;
    var url = "ajax/AjaxComponents.php?action=getComp&comp=" + compId;

    clearEditForm();

    if (compId == "0")
    {
        return;
    }

    xmlhttp = getXmlHttpObject();
    xmlhttp.onreadystatechange = componentStateChanged;
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
}

function clearEditForm()
{
    try
    {
        var form = document.getElementById("editProductForm");
        var htmlComponentList = document.getElementsByName("component[]");

        form.name.value = "";
        form.description.value = "";
        form.retailprice.value = "";
    }
    catch (error)
    {
        //do nothing
    }
}

function autoCompleteEditForm()
{
    var form = document.getElementById("editProductForm");
    var productId = form.productId.options[form.productId.options.selectedIndex].value;

    // auto complete the product data
    var productUrl = "ajax/AjaxProducts.php?action=getProduct&id=" + productId;
    xmlhttp = getXmlHttpObject();
    xmlhttp.onreadystatechange = productStateChanged;
    xmlhttp.open("POST", productUrl, true);
    xmlhttp.send(null);
}

function productStateChanged()
{
    if (xmlhttp.readyState == 4)
    {
        xml = xmlhttp.responseXML;
        
        var form = document.getElementById("editProductForm");
        form.name.value = xml.getElementsByTagName("name")[0].childNodes[0].nodeValue;
        form.description.value = xml.getElementsByTagName("description")[0].childNodes[0].nodeValue;
        form.retailprice.value = xml.getElementsByTagName("price")[0].childNodes[0].nodeValue;

        // auto select the menu indexs for the components
        // get the id's from xml and get the menus from the page
        var xmlComponentList = xml.getElementsByTagName("id");
        var htmlComponentList = document.getElementsByName("component[]");

        //loop through each of the components in the xml file
        for (var i = 0; i < xmlComponentList.length; i++)
        {
            // get the current xml component id
            var xmlComponentId = xmlComponentList[i].childNodes[0].nodeValue;

            // loop through the html menus to find a matching component id
            for (var j = 0; j < htmlComponentList.length; j++)
            {
                var newIndex = getSelectedIndex(htmlComponentList[j], xmlComponentId);

                // if id was found, update the menu to the next index
                if (newIndex != -1)
                    htmlComponentList[j].selectedIndex = newIndex;
            }
        }
    }
}